export interface Book {
    title: string,
    author: string,
    published: string,
    updated: string,
    language: string,
    issued: string,
    category: string,
    summary: string,
    extent: number,
    source: string,
    epubUrl: string,
    coverUrl: string,
}

export default function (item: any): Book {

    const category = item.category?.map((category: any) => {
        return category.$.label
    }).join(',')

    let epubUrl: string = '', coverUrl: string = ''

    item.link.forEach((link: any) => {
        if (link.$.type === "application/epub+zip") {
            epubUrl = link.$.href
        }
        if (link.$.type === "image/jpeg") {
            coverUrl = link.$.href
        }
    })

    return {
        title: item.title && item.title[0],
        author: item.author && item.author[0].name?.join(','),
        published: item.published && item.published[0],
        updated: item.updated && item.updated[0],
        language: item["dcterms:language"] && item["dcterms:language"][0],
        issued: item["dcterms:issued"] && item["dcterms:issued"][0],
        category: category,
        summary: item.summary && item.summary[0],
        extent: item['dcterms:extent'] && parseInt(item['dcterms:extent'][0].split(',').join('')),
        source: item['dcterms:source'] && item['dcterms:source'][0],
        epubUrl,
        coverUrl,
    }
}